package com.ellricky.teacher.ui.mycourse.leave

import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.dialog.picker.EPickerEntityListener
import com.ellricky.baselib.dialog.picker.EPickerStrListener
import com.ellricky.baselib.dialog.picker.EPickerType
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.teacher.API_LEAVE_SAVE
import com.ellricky.teacher.R
import kotlinx.android.synthetic.main.tc_activity_leave_apply.*
import org.json.JSONObject

class LeaveApplyActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),
        EPickerStrListener, EPickerEntityListener<IdNamePickerEntity> {


    private lateinit var ePickerUtil: EPickerUtil
    private var selClass: IdNamePickerEntity? = null
    private var selLeaveType: IdNamePickerEntity? = null
    private var selTeacher: IdNamePickerEntity? = null


    override fun getContentLayout(): Int {
        return R.layout.tc_activity_leave_apply
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.tc_leave_apply)

        ePickerUtil = EPickerUtil(this)

        etStartDate.setOnClickListener(this)
        etEndDate.setOnClickListener(this)
        etStartTime.setOnClickListener(this)
        etEndTime.setOnClickListener(this)

        etClass.setOnClickListener(this)
        etLeaveType.setOnClickListener(this)
        etTempTeacher.setOnClickListener(this)

        btnSubmit.setOnClickListener(this)
    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            etStartDate -> {
                val date = etStartDate.value
                ePickerUtil.showStartDatePicker(date, this)
            }
            etEndDate -> {
                val date = etEndDate.value
                ePickerUtil.showEndDatePicker(date, this)
            }

            etStartTime -> {
                val time = etStartTime.value
                ePickerUtil.showStartTimePicker(time, this)
            }
            etEndTime -> {
                val time = etEndTime.value
                ePickerUtil.showEndTimePicker(time, this)
            }

            etClass -> ePickerUtil.checkTeaClassPicker(selClass, this)
            etLeaveType -> ePickerUtil.checkTeaLeaveTypePicker(selLeaveType, this)
            etTempTeacher -> ePickerUtil.checkTeaTempPicker(selTeacher, this)

            btnSubmit -> postSubmit()
        }
    }

    override fun onSelectItem(content: String?, picPos: Int, pickType: Int) {
        when (pickType) {
            EPickerType.PICK_START_DATE -> etStartDate.value = content
            EPickerType.PICK_END_DATE -> etEndDate.value = content
            EPickerType.PICK_START_TIME -> etStartTime.value = content
            EPickerType.PICK_END_TIME -> etEndTime.value = content
        }
    }

    override fun onSelectItem(pickType: Int, t: IdNamePickerEntity?) {
        val name = t?.name
        when (pickType) {
            EPickerType.PICK_TEACHER_CLASS -> {
                selClass = t
                etClass.value = name
            }
            EPickerType.PICK_TEACHER_LEAVE_TYPE -> {
                selLeaveType = t
                etLeaveType.value = name
            }
            EPickerType.PICK_TEMP_TEACHER -> {
                selTeacher = t
                etTempTeacher.value = name
            }
        }
    }


    private fun postSubmit() {
        val startDate = etStartDate.value
        if (EmptyUtil.isEmpty(startDate)) {
            ToastUtil.show(this, R.string.tc_leave_date_start_select)
            return
        }

        val endDate = etEndDate.value
        if (EmptyUtil.isEmpty(endDate)) {
            ToastUtil.show(this, R.string.tc_leave_date_end_select)
            return
        }

        val startTime = etStartTime.value
        if (EmptyUtil.isEmpty(startTime)) {
            ToastUtil.show(this, R.string.tc_leave_time_start_select)
            return
        }

        val endTime = etEndTime.value
        if (EmptyUtil.isEmpty(endTime)) {
            ToastUtil.show(this, R.string.tc_leave_time_end_select)
            return
        }

        val cla = etClass.value
        if (EmptyUtil.isEmpty(cla)) {
            ToastUtil.show(this, R.string.tc_class_select_please)
            return
        }

        val leaveType = etLeaveType.value
        if (EmptyUtil.isEmpty(leaveType)) {
            ToastUtil.show(this, R.string.tc_leave_type_select)
            return
        }

        val leaveReason = etLeaveReason.value
        if (EmptyUtil.isEmpty(leaveReason)) {
            ToastUtil.show(this, R.string.tc_leave_reason_input)
            return
        }

        val tempTeacher = etTempTeacher.value
        if (EmptyUtil.isEmpty(tempTeacher)) {
            ToastUtil.show(this, R.string.tc_class_temp_teacher_select)
            return
        }

        val paramMap = mutableMapOf<String, Any>(
                "startDate" to startDate,
                "endDate" to endDate,
                "startTime" to startTime,
                "endTime" to endTime,
                "classId" to "${selClass?.id}",
                "leaveType" to "${selLeaveType?.id}",
                "leaveReson" to leaveReason,
                "changeTeacherId" to "${selTeacher?.id}"
        )


        val url = EAppUtil.getApiHost() + API_LEAVE_SAVE
        HttpUtils.getInstance().post(url, paramMap, object : JOCallback(this) {
            override fun onSuccess(dataJO: JSONObject?, msg: String?) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }
        })

    }
}

